iT邦幫忙

2023 iThome 鐵人賽

DAY 6
0
自我挑戰組

設計模式系列 第 6

Day6 - 工廠模式: 抽象工廠模式(Abstract factory pattern)

  • 分享至 

  • xImage
  •  

介紹
抽象工廠模式類似於生產家族產品的概念,像是麥當勞與肯德基皆是速食店,皆生產類似的產品(ex.漢堡、薯條..),但兩者的漢堡與薯條肯定不會相同,使用的麵包、醬料等都有各自的祖傳祕方。此時可應用抽象工廠模式來創建各自的產品。

C++範例

#include <iostream>

// 抽象漢堡類
class Hamburger
{
public:
    virtual void PrintSecretFormula() = 0;
    virtual ~Hamburger() {}
};

// 抽象薯條類
class FrenchFries
{
public:
    virtual void PrintSecretFormula() = 0;
    virtual ~FrenchFries() {}
};

// 麥當勞漢堡秘方實作
class McDonaldsHamburger : public Hamburger
{
public:
    void PrintSecretFormula() override
    {
        std::cout << "Contains McDonalds hamburger secret formula" << std::endl;
    }
};

// 麥當勞薯條秘方實作
class McDonaldsFrenchFries : public FrenchFries
{
public:
    void PrintSecretFormula() override
    {
        std::cout << "Contains McDonalds french fries secret formula" << std::endl;
    }
};

// 肯德基漢堡秘方實作
class KFCHamburger : public Hamburger
{
public:
    void PrintSecretFormula() override
    {
        std::cout << "Contains KFC hamburger secret formula" << std::endl;
    }
};

// 肯德基薯條秘方實作
class KFCFrenchFries : public FrenchFries
{
public:
    void PrintSecretFormula() override
    {
        std::cout << "Contains KFC french fries secret formula" << std::endl;
    }
};

// 抽象工廠
class Factory
{
public:
    virtual Hamburger *CreateHamburger() = 0;
    virtual FrenchFries *CreateFrenchFries() = 0;
};

// 麥當勞工廠
class McDonaldsFactory : public Factory
{
public:
    Hamburger *CreateHamburger()
    {
        return new McDonaldsHamburger();
    }

    FrenchFries *CreateFrenchFries()
    {
        return new McDonaldsFrenchFries();
    }
};

// 肯德基工廠
class KFCFactory : public Factory
{
public:
    Hamburger *CreateHamburger()
    {
        return new KFCHamburger();
    }

    FrenchFries *CreateFrenchFries()
    {
        return new KFCFrenchFries();
    }
};

int main()
{
    //
    // 麥當勞
    //

    // 建立麥當勞工廠
    Factory *mcFactory = new McDonaldsFactory();

    // 生產麥當勞漢堡及薯條
    Hamburger *mcHamburger = mcFactory->CreateHamburger();
    FrenchFries *mcFrenchFries = mcFactory->CreateFrenchFries();

    // 印出祖傳秘方
    mcHamburger->PrintSecretFormula();
    mcFrenchFries->PrintSecretFormula();

    delete mcFrenchFries;
    delete mcHamburger;
    delete mcFactory;

    //
    // 肯德基
    //

    // 建立肯德基工廠
    Factory *kfcFactory = new KFCFactory();

    // 生產肯德基漢堡及薯條
    Hamburger *kfcHamburger = kfcFactory->CreateHamburger();
    FrenchFries *kfcFrenchFries = kfcFactory->CreateFrenchFries();

    // 印出祖傳秘方
    kfcHamburger->PrintSecretFormula();
    kfcFrenchFries->PrintSecretFormula();

    delete kfcFrenchFries;
    delete kfcHamburger;
    delete kfcFactory;

    return 0;
}

Output:

Contains McDonalds hamburger secret formula
Contains McDonalds french fries secret formula
Contains KFC hamburger secret formula
Contains KFC french fries secret formula

上一篇
Day5 - 工廠模式: 工廠方法模式(Factory method pattern)
下一篇
Day7 - 建造模式(Builder pattern)
系列文
設計模式30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言